// packageJ/love/loveWaitReleases/loveWaitReleases.js
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    releaseInfo: {},
    dataList: [],
    titleArr: ['固定释放记录','加速释放记录'],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.activeName = 0;
    this.data.dataList = [];
    this.data.titleArr.forEach((val, index) => {
      this.data.dataList.push({
        isLoadMore: true,
        page: 1,
        total_page: 0,
        list: [],
      });
    });
    this.getData();
  },

  getData(event) {
    if (event) {
      this.activeName = event.detail.name;
    }

    let urlStr = app.getNetAddresss('plugin.love-release.frontend.controllers.index.index');
    app._postNetWork({
      url: urlStr,
      data: {
        release_type: this.activeName + 1
      },
      success: (resdata) => {
        wx.hideLoading();
        let res = resdata.data;
        if (res.result == 1) {
          let { list, ...other } = res.data;
          wx.setNavigationBarTitle({
            title: `待释放${other.diy_name}`
          })
          this.isLoadMore = true;
          this.data.dataList[this.activeName].page = 1;
          this.data.dataList[this.activeName].isLoadMore = true;
          this.data.dataList[this.activeName].total_page = list.last_page;
          this.setData({
            releaseInfo: other,
            ['dataList[' + this.activeName + '].list']: list.data,
          });
        } else {
          wx.showToast({
            title: res.msg,
            duration: 1000,
            icon: 'none'
          });
        }
      },
      fail: function (res) {
        wx.hideLoading();
      }
    });
  },
  _getMoreData() {
    this.data.dataList[this.activeName].isLoadMore = false;
    if (this.data.dataList[this.activeName].page >= this.data.dataList[this.activeName].total_page) {
      return;
    } else {
      this.data.dataList[this.activeName].page = this.data.dataList[this.activeName].page + 1;
      let urlStr = app.getNetAddresss('plugin.love-release.frontend.controllers.index.index');
      urlStr += "&page=" + this.data.dataList[this.activeName].page;
			
      app._getNetWork({
        url: urlStr,
        data:{ release_type: this.activeName + 1 },
        success: (resdata)=> {
          var res = resdata.data;
          if (res.result == 1) {
            let myData = res.data.list;
            this.data.dataList[this.activeName].isLoadMore = true;
            this.setData({
              ['dataList[' + this.activeName + '].list']: this.data.dataList[this.activeName].list.concat(myData.data)
            });
          }else{
            this.data.dataList[this.activeName].isLoadMore = false;
            this.setData({
              ['dataList[' + this.activeName + '].page']: this.data.dataList[this.activeName].page -1
            });
          }
        },
        fail: function(res) {
          console.log(res);
        }
      });
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (this.data.dataList[this.activeName].isLoadMore) {
      this._getMoreData();
    } else {
      console.log('没有更多数据');
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})